/**
 * Created by lx on 2017/5/1.
 */
layui.use(['form'], function () {
    var form = layui.form();
    var market = {};
    $(document).ready(function () {
        if (window.location.hash != null && window.location.hash != "") {
            var has = window.location.hash;
            has = has.replace("#", "");
            has = has.replace(/\'/g, "\"");
            market = JSON.parse(has);
            if(isNotNull(market.id)){
                $.ajax({
                    type: "POST",
                    url: "/marketInfo/list",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(market),
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $('#flowList').append(createCollaMarketInfoItem(data.param)) ;
                            form.render() ;
                        } else {
                            layer.msg("获取订单信息失败");
                        }
                    },
                    error: function (message) {
                        layer.msg("获取订单信息失败");
                    }
                });
            }
        }
        form.on('submit(yes)', function (data) {
            var  url = "/marketInfo/update";
            var list={marketInfoDOs: []} ;
            $('.layui-input').each(function(item,element){
                list.marketInfoDOs.push({id:element.id,value:element.value}) ;
            });
            if(isNotNull(market.customerId)&&isNotNull(market.businessId)){
                $.ajax({
                    type: "POST",
                    url: url,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(list),
                    dataType: "json",
                    success: function (data){
                        if(data.success){
                            layer.msg("增加订单成功") ;
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }else{
                            layer.msg("增加订单失败") ;
                        }
                    },
                    error: function (message) {
                        layer.msg("增加订单失败") ;
                    }
                });
            }else {
                layer.msg("请选择客户和业务") ;
            }
            return false;
        });

        $('#cancel').click(function(){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            return false ;
        }) ;
    });
});
